package com.bdqn.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.base.R;
import com.bdqn.base.logger.WebLogger;
import com.bdqn.pojo.ShopUserAccount;
import com.bdqn.pojo.ShopUserAccountCountRateVO;
import com.bdqn.service.ShopUserAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户账户
 *
 * @author lilibo
 * @date 2021/10/01
 */
@Api(tags="用户账户接口")
@RestController
@RequestMapping("/shopuseraccount")
public class ShopUserAccountController {

    @Autowired
    private ShopUserAccountService shopUserAccountService;

    /**
     * 新增用户账户记录
     *
     * @param shopUserAccount
     * @return R
     */
    @ApiOperation("新增用户账户记录")
    @WebLogger("新增用户账户记录")
    @PostMapping("/add")
    public R save(@ApiParam("用户账户对象") @RequestBody ShopUserAccount shopUserAccount) {
        return new R<>(shopUserAccountService.save(shopUserAccount));
    }

    /**
     * 修改用户账户记录
     *
     * @param shopUserAccount
     * @return R
     */
    @ApiOperation("修改用户账户记录")
    @WebLogger("修改用户账户记录")
    @PostMapping("/update")
    public R update(@ApiParam("用户账户对象") @RequestBody ShopUserAccount shopUserAccount) {
        return new R<>(shopUserAccountService.updateById(shopUserAccount));
    }

    /**
     * 通过id删除一条用户账户记录
     *
     * @param id
     * @return R
     */
    @ApiOperation("通过id删除一条用户账户记录")
    @WebLogger("通过id删除一条用户账户记录")
    @PostMapping("/delete")
    public R removeById(@ApiParam("用户账户id") Integer id) {
        return new R<>(shopUserAccountService.removeById(id));
    }

    /**
     * 通过多个id批量删除用户账户记录
     *
     * @param ids
     * @return R
     */
    @ApiOperation("通过多个id批量删除用户账户记录")
    @WebLogger("通过多个id批量删除用户账户记录")
    @PostMapping("/remove")
    public R removeByIds(@ApiParam("用户账户id集合") @RequestBody List<Integer> ids) {
        return new R<>(shopUserAccountService.removeByIds(ids));
    }

    /**
     * 通过id查询单条用户账户记录
     *
     * @param id
     * @return R
     */
    @ApiOperation("通过id查询单条用户账户记录")
    @WebLogger("通过id查询单条用户账户记录")
    @GetMapping("/{id}")
    public R<ShopUserAccount> getById(@ApiParam("用户账户id") @PathVariable Integer id) {
        return new R<>(shopUserAccountService.getById(id));
    }

    /**
     * 通过用户账户信息查询用户账户记录
     *
     * @param shopUserAccount 用户账户信息对象
     * @return R
     */
    @ApiOperation("通过用户账户信息查询用户账户记录")
    @WebLogger("通过用户账户信息查询用户账户记录")
    @GetMapping("/list")
    public R<List<ShopUserAccount>> getList(@ApiParam("查询的用户账户对象") ShopUserAccount shopUserAccount) {
        return new R<>(shopUserAccountService.getList(shopUserAccount));
    }

    /**
     * 分页查询用户账户
     *
     * @param pageNo 当前页
     * @param pageSize 每页条数
     * @param shopUserAccount 用户账户
     * @return
     */
    @ApiOperation("分页查询用户账户")
    @WebLogger("分页查询用户账户")
    @GetMapping("/page")
    public R<IPage<ShopUserAccount>> getPage(@ApiParam("当前页") @RequestParam(required = false, defaultValue = "1")  Long pageNo, @ApiParam("每页条数") @RequestParam(required = false, defaultValue = "10") Long pageSize, @ApiParam("查询的用户账户对象") ShopUserAccount shopUserAccount) {
        return new R<>(shopUserAccountService.getPage(new Page<>(pageNo, pageSize), shopUserAccount));
    }

    /**
     * 分页查询用户账户数据
     *
     * @param pageNo 当前页
     * @param pageSize 每页条数
     * @param shopUserAccount 用户账户
     * @return 返回对象中的count为数据总量
     */
    @ApiOperation("分页查询用户账户数据")
    @WebLogger("分页查询用户账户数据")
    @GetMapping("/pagelist")
    public R<List<ShopUserAccount>> getPageList(@ApiParam("当前页") @RequestParam(required = false, defaultValue = "1")  Long pageNo, @ApiParam("每页条数") @RequestParam(required = false, defaultValue = "10") Long pageSize, @ApiParam("查询的用户账户对象") ShopUserAccount shopUserAccount) {
        IPage<ShopUserAccount> page = shopUserAccountService.getPage(new Page<>(pageNo, pageSize), shopUserAccount);
        return new R<>(page.getRecords(), page.getTotal());
    }


    /**
     * 用户数统计
     *
     * @return
     */
    @ApiOperation("账户余额数统计")
    @WebLogger("账户余额数统计")
    @GetMapping("/countRate")
    public R<ShopUserAccountCountRateVO> getCountRate() {
        return new R<>(shopUserAccountService.getCountRate());
    }

}
